package net.messagenet.mazor.infra.persistence;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;

import net.messagenet.mazor.DO.CustomerDO;
import net.messagenet.mazor.DO.DpaDO;
import net.messagenet.mazor.DO.DpaProductDO;
import net.messagenet.mazor.DO.PosDO;
import net.messagenet.mazor.DO.PosProductDO;
import net.messagenet.mazor.exception.MZRApplicativeException;
import net.messagenet.mazor.infra.cache.CacheManager;

/**
 * This interface performs all the mazor request to the database
 * 
 * @author Idan
 *
 */

public interface PersistenceManager {

	/**
	 * This method called by the engine is for seeting the entityManager
	 * 
	 * @param em
	 */
	public void setEntityManager(EntityManager em);
	
	
	/**
	 * Saves the full dpa request in the database
	 * 
	 * @param dpa
	 */
	void saveDPARequest(DpaDO dpa);



	/**
	 * 
	 * This method updates the expiration date and the dpa number for an existing dpa
	 * request
	 * it also updates all the approved prices of the products in the dpa
	 * 
	 * @param dpaId
	 * @param dpaRequestId
	 * @param experationDate
	 * @param approvedPrices 
	 * @throws MZRApplicativeException
	 */
	public void updateDPARequest(String dpaId, int dpaRequestId,
			Date experationDate, LinkedList<Double> approvedPrices) throws MZRApplicativeException;
	/**
	 * This method sets the cache manger
	 * when this method is called the cache manager is filled 
	 * 
	 * @param CacheManager
	 */
	public void setCacheManager (CacheManager cacheManager);

	/**
	 * This method makes the the manager to fill the cahce with data
	 * 
	 */
	public void fillCache();

	/**
	 * This method gets all the open dpas that didn't expired and has more product to report
	 * 
	 * @param context
	 * @return a map of dpas keyed by the dialogic item #
	 */
	public Map<String, DpaDO> getAllApprovedDPAs();

	/**
	 * This method save the pos request to the DB
	 * 
	 * @param posDo
	 */

	public void savePOSrequest(PosDO posDo);


	/**
	 * This method updates the DB for "using" a product.
	 * it decreases the product on DPA according to the quantity of the POS
	 * 
	 * @param posProducts the products from the pos
	 */
	public void updateQuantityOnDPA(List<PosProductDO> posProducts);


	/**
	 * This method creates a new customer record in the DB if the id is 0
	 * if not it edits all the field
	 * 
	 * @param customer
	 */
	public void createOrEditCustomer(CustomerDO customer);
	/**
	 * This method returns all the products in the dpa that were reported in the pos and didn't
	 * payment
	 * 
	 * @param ctx
	 * @param fromDate
	 * @param toDate
	 * @return a list of all the products
	 */

	public List<DpaProductDO> getDPAReportedProducts(Date fromDate, Date toDate);


	/**
	 * This method updates the paid quantity in the dpa products after refunding
	 * 
	 * 
	 * @param ctx
	 * @param products
	 */
	public void updatePaidProducts(List<DpaProductDO> products);


	/**
	 * This method saves the dpa object asa a draft that can loaded later
	 * 
	 * @param dpa
	 */
	public void saveDPADraft(DpaDO dpa);
	/**
	 *
	 * returns all the dpa drafts
	 * 
	 * @return all the dpas marked as draft
	 */

	public List<DpaDO> getDPADrafts();

	/**
	 * given a list of dpa product this method removes those products form the DB
	 * 
	 * 
	 * @param removePrds
	 */

	public void removeDPAProducts(List<DpaProductDO> removePrds);
	/**
	 * returns all the pos drafts
	 * 
	 * @return a list of all pos defined as drafts
	 */

	public List<PosDO> getPOSDrafts();
	/**
	 * This method saves the dpa object as a draft that can loaded later
	 * 
	 * @param pos pos data object
	 */
	public void savePOSDraft(PosDO pos);


	/**
	 * given a list of pos product this method removes those products form the DB
	 * 
	 * 
	 * @param removePrds
	 */

	public void removePOSProducts(List<PosProductDO> removePrds);
	/**
	 * gets all the dpas that are not closed (has didn't receive full payment for all the products )
	 * 
	 * @param ctx
	 * @return a map of dpas keyed by the dialogic item #
	 */

	public Map<Integer, DpaDO> getAllNotClosedDPAs();


	/**
	 * This method returns all the closed dpa and not approved dpas
	 * 
	 * @param ctx
	 * @return
	 */
	public List<DpaDO> getClosedAndNotApprovedDPAs();

	/**
	 * This method returns all the DPAs in the system form draft to new DPA
	 * mapped by id
	 * 
	 * @return 
	 */
	public Map<Integer, DpaDO> getAllDPAs();
}
